# payment_page.py
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class PaymentPage:
    def __init__(self, driver):
        self.driver = driver
        self.url = 'http://example.com/payment'
        self.payment_method_selector_locator = (By.ID, 'payment-method-selector')
        self.card_number_input_locator = (By.ID, 'card-number')
        self.expiry_date_input_locator = (By.ID, 'expiry-date')
        self.cvv_input_locator = (By.ID, 'cvv')
        self.place_order_button_locator = (By.ID, 'place-order-button')
        self.order_confirmation_locator = (By.ID, 'order-confirmation')
        self.payment_error_message_locator = (By.ID, 'payment-error-message')

    # 导航到支付页面
    def navigate(self):
        """Navigate to the payment page."""
        self.driver.get(self.url)

    # 选择支付方式
    def select_payment_method(self, method):
        """Select a payment method from the dropdown."""
        payment_method_selector = self.driver.find_element(*self.payment_method_selector_locator)
        payment_method_selector.send_keys(method)

    # 输入信用卡号
    def enter_card_number(self, card_number):
        """Enter the credit card number."""
        card_number_input = self.driver.find_element(*self.card_number_input_locator)
        card_number_input.send_keys(card_number)

    # 输入过期日期
    def enter_expiry_date(self, expiry_date):
        """Enter the expiry date of the credit card."""
        expiry_date_input = self.driver.find_element(*self.expiry_date_input_locator)
        expiry_date_input.send_keys(expiry_date)

    # 输入 CVV
    def enter_cvv(self, cvv):
        """Enter the CVV code of the credit card."""
        cvv_input = self.driver.find_element(*self.cvv_input_locator)
        cvv_input.send_keys(cvv)

    # 点击确认订单按钮
    def click_place_order(self):
        """Click the place order button."""
        place_order_button = self.driver.find_element(*self.place_order_button_locator)
        place_order_button.click()

    # 等待订单确认显示
    def wait_for_order_confirmation(self):
        """Wait for the order confirmation message to appear."""
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.order_confirmation_locator)
        )

    # 获取订单确认消息
    def get_order_confirmation_message(self):
        """Get the order confirmation message."""
        return self.driver.find_element(*self.order_confirmation_locator).text

    # 获取支付错误消息
    def get_payment_error_message(self):
        """Get the payment error message if there is one."""
        return self.driver.find_element(*self.payment_error_message_locator).text